package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysFunctionDaoI;
import sy.model.SysFunction;
import sy.util.Menu;

@Repository("sysFunctionDao")
public class SysFunctionDaoImpl implements SysFunctionDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysFunction sysFunction) {
		return getCurrentSession().save(sysFunction);//直接用
		
	}

	@Override
	public void remove(SysFunction sysFunction) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysFunction);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysFunctionDaoI#update(sy.model.SysFunction)
	 */
	@Override
	public void update(SysFunction sysFunction) {
		// TODO Auto-generated method stub
		getCurrentSession().update(sysFunction);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysFunctionDaoI#list()
	 */
	@Override
	public List<SysFunction> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysFunction").list();
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysFunctionDaoI#getSysFunctionById(java.lang.String)
	 */
	@Override
	public SysFunction getSysFunctionById(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysFunction where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysFunction> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysFunctionDaoI#listSysFunctionBykey(java.lang.String)
	 */
	@Override
	public List<SysFunction> listSysFunctionBykey(String key) {
		// TODO Auto-generated method stub
		String hql = "from SysFunction where code like :key or name like :key";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		List<SysFunction> list = query.list();
		return list;
	}

	@Override
	public List<Menu> listMenuByType(String user, String type,
			String functionPid) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
	    sb.append("select d.id id, d.code code, d.name name, d.url url, d.pid pid, c.enabled enabled from sys_account a left join sys_role_account_mapping b on a.id = b.account ")
	        .append("left join sys_role_function_mapping c on b.role = c.role left join sys_function d on c.sysFunction = d.id where a.name = :user ");
	    if ("parent".equals(type)) {
	      sb.append("and d.pid is null ");
	    } else {
	      sb.append("and d.pid in (:functionPid) ");
	    }
	    SQLQuery query = getCurrentSession().createSQLQuery(sb.toString());
	    query.setString("user", user);
	    if (!"parent".equals(type)) {
	      query.setParameterList("functionPid", functionPid.split(","));
	    } else {
	      
	    }
	    query.addScalar("id")
	    .addScalar("code")
	    .addScalar("name")
	    .addScalar("url")
	    .addScalar("pid")
	    .addScalar("enabled")
	    .setResultTransformer(Transformers.aliasToBean(Menu.class));
	    List<Menu> list = query.list();
	    return list;
	}

}
